Developing applications that work with SQL Azure
isn't rocket science, but it requires knowing what to expect and what
functionality you have to work with. You read earlier that not all
client libraries work with SQL Azure and saw the libraries that are
supported.
This section looks at using
several technologies to connect to and query a SQL Azure database,
including ADO.NET, ODBC, and WCF Data Services. You read at length
earlier about taking the right approach to move to the Azure platform.
You must consider many things, including the following:
SQL Azure is only available via TCP port 1433.
SQL Azure doesn't currently support OLE DB.
SQL Azure only supports SQL Server authentication. Windows Authentication isn't supported.
When
connecting to SQL Azure, you must specify the target database in the
connection string. Otherwise, you're connecting to the master database.
Distributed
transactions (transactions that affect multiple resources, such as
tables, or different databases via sharding) aren't supported in SQL
Azure.
You must ensure that your SQL Azure firewall is configured to accept connections.
You must determine whether any embedded (in-line) T-SQL in your application is supported by SQL Azure.
You must use the login name format <login>@<server> when connecting to SQL Azure, because some tools implement Tabular Data Stream (TDS) differently.
Let's get to some coding.
The next few sections show you how to connect to SQL Azure using
different libraries such as ADO.NET, ODBC, the sqlcmd utility, and WCF
Data Services to query SQL Azure.
1. ADO.NET
Microsoft makes it very easy
to connect an application to SQL Azure by providing the necessary
connection strings for both ADO.NET and ODBC, as shown in Figure 1.
You can find the connection information on the SQL Azure Server
Administration page by selecting a database and clicking the Connection
Strings button.
1.1. Making the Connection
Let's first look at how to
connect to an Azure database using ADO.NET. Fire up an instance of
Visual Studio 2010, and create a new Windows Forms application. Then,
follow these steps:
Place a button on Form1, and double-click the new button to view its click event.
Before
you place any code in the click event, let's add a method to get a
connection string. To demonstrate connecting to SQL Azure versus a local
database, let's first connect to a local copy of the database. Then,
you can change to connect to Azure. Below the click event, add a new
method called GetConString that returns the connection string for your local instance of SQL Server. Here's the code to write:
string GetConString()
{
return "Server=server;Database=TechBio;User ID=sa;Password=password;";
}
Go back to the button's click event, and add the following code. This code calls the GetConString
method you previously added, returns the connection string, establishes
and opens a connection to the local database, and then closes the
connection:
private void button1_Click(object sender, EventArgs e)
{
string connStr = GetConString();
using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
conn.Open();
MessageBox.Show("Connection made.");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
conn.Close();
}
}
}
Run the application, and click the button on the form. You should get a message box that says "Connection Made."
Now, let's change this simple
application to connect to SQL Azure. Instead of returning the connection
string to your local database, you want to return the ADO.NET
connection string. Continue as follows:
On your SQL Azure Server Administration page, select the database you want to connect to.
Click the Connection Strings button, and click the Copy to Clipboard link for the ADO.NET connection string.
Back in your Visual Studio project, replace the local connection string in the GetConString
method with the SQL Azure ADO.NET connection string, as shown in the
following code. Be sure to enter your correct password into the
connection string:
string GetConString()
{
return "Server=tcp:servername.database.windows.net;Database=TechBio;
UserID=SQLScott@servername;Password=password;
Trusted_Connection=False;Encrypt=True;";
}
Before
you run the application, make sure your Azure firewall settings are up
to date (via the SQL Azure Server Administration page). Then, run the
application, and click the button on the form. If everything is
configured correctly, you should get a message box that says "Connection
Made."
Granted, this is a very
simple example, but it illustrates how easy it is to take an existing
application and point it to SQL Azure. The caveat is what your
application contains. As mentioned earlier, if you have any inline
T-SQL, you at a minimum need to ensure that your inline T-SQL is
supported by SQL Azure. The likelihood is that it is, but it's always
safest to check and test.
Even though you've connected
to SQL Azure, does that affect your data-access code? The next two
sections discuss using a data reader and a dataset when connecting to
SQL Azure.